package com.ws.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ws.commonutils.R;
import com.ws.eduservice.entity.EduTeacher;
import com.ws.eduservice.entity.vo.TeacherQuery;
import com.ws.eduservice.service.EduTeacherService;
import com.ws.servicebase.exceptionhandler.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ws
 * @since 2024-11-22
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    //http://localhost:8001/eduservice/teacher/findAll

    @Autowired
    private EduTeacherService eduTeacherService;
    //查询所有讲师
    @GetMapping("findAll")
    public R findAll() {

//        try {
//            int i=10/0;
//        }catch (Exception e){
//           throw new MyException(20001,"执行自定义的异常");
//        }

        List<EduTeacher> eduTeachers = eduTeacherService.list(null);
        return R.ok().data("items", eduTeachers);
    }

    //根据id删除讲师
    @DeleteMapping("deleteTeacher/{id}")
    public R deleteTeacher(@PathVariable String id) {
        boolean flag = eduTeacherService.removeById(id);
        System.out.println(flag);
        if (!flag) {
            return R.error();
        }
            return R.ok();

    }
    //
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageTeacher(@PathVariable Long current,@PathVariable Long limit) {
        Page<EduTeacher> page =new Page<>(current,limit);
        eduTeacherService.page(page,null);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        return R.ok().data("total", total).data("items", records);
    }
//分页条件查询
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable Long current,@PathVariable Long limit,@RequestBody(required = false) TeacherQuery teacherquery) {
        Page<EduTeacher> page =new Page<>(current,limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(teacherquery.getName())) {
            wrapper.like("name",teacherquery.getName());
        }
        if (!StringUtils.isEmpty(teacherquery.getLevel())){
            wrapper.eq("level",teacherquery.getLevel());
        }
        if (!StringUtils.isEmpty(teacherquery.getBegin())){
            wrapper.ge("gmt_create",teacherquery.getBegin());
        }
        if (!StringUtils.isEmpty(teacherquery.getEnd())){
            wrapper.le("gmt_create",teacherquery.getEnd());
        }
        wrapper.orderByDesc("gmt_create");
        eduTeacherService.page(page,wrapper);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        System.out.println(records);
        return R.ok().data("total", total).data("items", records);
    }
//添加讲师
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.save(eduTeacher);
        if (!flag) {
            return R.error();
        }else {
            return R.ok();
        }
    }
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("eduTeacher", eduTeacher);
    }
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (!flag) {
            return R.error();
        }else {
            return R.ok();
        }
    }
}

